package fr.umlv.jee.jawp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.umlv.jee.jawp.service.aws.Aws;

@Service("buyerProductService")
public class BuyerProductService{

  private int idBuyer;
  private int idProduct;

  public int getIdBuyer() {
	return idBuyer;
  }

  public void setIdBuyer(int idBuyer) {
	this.idBuyer = idBuyer;
  }

  public int getIdProduct() {
    return idProduct;
  }

  public void setIdProduct(int idProduct) {
	this.idProduct = idProduct;
  }

  @Transactional
  public List<Map<String, String>> findArticleByIdBuyer(String idBuyer) {
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    
    Map<String, String> m1 = new HashMap<String, String>();
    m1.put("idProduct", "0545010225");
    m1.put("Type produit", "Books");
    m1.put("Title", "Java spring");
    m1.put("Qte en stock", "12");
    m1.put("Qte ordered", "0");
    list.add(m1);
    
    Map<String, String> m2 = new HashMap<String, String>();
    m2.put("idProduct", "2212113463");
    m2.put("Type produit", "Books");
    m2.put("Title", "Il est venu libéré les captifs");
    m2.put("Qte en stock", "12");
    m2.put("Qte ordered", "6");
    list.add(m2);
    
    return list;
  }

  @Transactional
  public int countArticle(List<Map<String, String>> listArticle) {
	  System.out.println("Nombre de paroduct" + listArticle.size());
	  return listArticle.size();
  }
  @Transactional
  public Map<String, String> findInfoArticleByIds(String userName, String idProduct) {
	  	System.out.println("IdProducts: " + idProduct);
	    Map<String, String> detailProduct = new HashMap<String, String>();	
	    detailProduct.put("idBuyer", userName);
	    detailProduct.put("idProduct", idProduct);
	    detailProduct.put("Price", "12$");
	    //look for some needed information through amazon API
	    Aws aws = new Aws();
	    List<Map<String, String>> detailsProduct = aws.findArticleByItemID(idProduct);
	    detailProduct.put("LargeImage.URL", detailsProduct.get(0).get("LargeImage.URL"));
	    return detailProduct;
  }  

  @Transactional
  public void saveProductOrder(String userName, String idProduct, int qtyOrdered) {
      System.out.println("Save Product Order: ");
      System.out.println("User name: " + userName + " idProduct: " + idProduct + " Qte: " + qtyOrdered);
  }

  @Transactional
  public void validateProductOrder(String userName, String idProduct, int qtyOrdered) {
      System.out.println("Validate Product Order: ");
      System.out.println("User name: " + userName + " idProduct: " + idProduct + " Qte: " + qtyOrdered);
  }

  @Transactional
  public boolean isOrderPresaved(int qtyOrdered) {
	  System.out.println("isOrderPresaved: " + qtyOrdered);
      if(qtyOrdered == 0)
    	  return false;
      else
    	  return true;
  }

  @Transactional
  public void deleteProductOrder(String userName, String idProduct) {
      System.out.println("Delete Product Order: ");
      System.out.println("User name: " + userName + " idProduct: " + idProduct );
  }
  
  @Transactional
  public String getInfoTitle(Map<String, String> map) {
    return map.get("Title");
  }
}